package com.ctrl.riveradministrator.mvp.view.activity.bulletin;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.os.Message;
import android.support.annotation.RequiresApi;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.ctrl.riveradministrator.R;
import com.ctrl.riveradministrator.base.BaseFragment;
import com.ctrl.riveradministrator.mvp.model.Bulletin;
import com.ctrl.riveradministrator.mvp.presenter.BulletinPresenter;

import java.util.List;

// TODO: 2019/5/17 未完成分页加载和刷新
public class BulletinFragment extends BaseFragment<BulletinPresenter> implements BulletinView {

    private Toolbar toolbar;
    private TextView titleTv;
    private ImageView leftMenu;
    private DrawerLayout drawerLayout;
    private RecyclerView bulletinRl;
    private SwipeRefreshLayout bulletinSfl;
    private LinearLayoutManager layoutManager;
    private BulletinAdapter bulletinAdapter;
    public static BulletinFragment INSTANCE;

    public BulletinFragment() {

    }


    public static BulletinFragment newInstance() {
        if (INSTANCE == null) {
            INSTANCE = new BulletinFragment();
        }
        return INSTANCE;
    }


    @Override
    protected int bindLayout() {
        return R.layout.fragment_bulletin;
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void initView(View view) {
        toolbar = $(view, R.id.toolbar);
        titleTv = $(view, R.id.title_tv);
        leftMenu = $(view, R.id.left_iv);
        bulletinRl = $(view, R.id.bulletin_list);
        bulletinSfl = $(view, R.id.bulletin_srl);
        //drawerLayout = getActivity().findViewById(R.id.drawer);

        layoutManager = new LinearLayoutManager(getContext());
        bulletinRl.setLayoutManager(layoutManager);
        bulletinSfl.setColorSchemeColors(Color.RED, Color.BLUE);
        bulletinAdapter = new BulletinAdapter(getContext());
        bulletinRl.setAdapter(bulletinAdapter);

        /*
         * 设置toolbar
         * */
        toolbar.setTitleTextColor(Color.WHITE);
        toolbar.setTitle("");
        titleTv.setText(getContext().getString(R.string.bulletin));
        leftMenu.setBackground(getContext().getDrawable(R.drawable.ic_menu_24dp));
    }


    @Override
    protected void doBusiness(Context mContext, Activity activity) {
        mPresenter.getBulletin("0", "20");
    }

    @Override
    protected void widgetClick(View view) {
        switch (view.getId()) {
            case R.id.left_iv:
                drawerLayout.openDrawer(GravityCompat.START);
                break;
        }
    }

    @Override
    protected void setListener() {
        leftMenu.setOnClickListener(this);
    }

    @Override
    public void handleMessage(Message message, int what) {

    }

    @Override
    public void showMessage(List<Bulletin> bulletins) {
        bulletinAdapter.updateListView(bulletins);
    }

    @Override
    protected BulletinPresenter createPresenter() {
        return new BulletinPresenter(this);
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        //使用是否对用户可见来判断是否进入该fragment，对该fragment重新加载
        if (!hidden) {
            mPresenter.getBulletin("0", "20");
        }
    }

}
